'use strict';
const { MongoDB } = require('../../config/config');
const mongoose = require('mongoose');

try {
  mongoose.connect(MongoDB.host, MongoDB.options).then(res => {
    console.log('连接成功');
  });
} catch (error) {
  // console.log(error);
  console.log('连接失败');
}

mongoose.connection.on('connected', function() {
  console.log('Mongoose connection open to ' + MongoDB.host);
});
/**
 * 连接异常 error 数据库连接错误
 */
mongoose.connection.on('error', function(err) {
  console.log('Mongoose connection error: ' + err);
});
/**
 * 连接断开 disconnected 连接异常断开
 */
mongoose.connection.on('disconnected', function() {
  console.log('Mongoose connection disconnected');
});

module.exports = mongoose;
